<?php

namespace app\helpers;

use app\components\Language;

class Translate {
    const PRODUCT_PREFIX = 'product_';

    public static function translate_label(){
        return [
            'en'=>'英文',
            'zh'=>'中文',
            'th'=>'泰文'
        ];
    }

    public static function get_translate_val($model_name,$id,$field,$lang){
        $text_key = self::PRODUCT_PREFIX.$model_name.'_'.$id.'_'.$field.'_'.$lang.'_';
        $redis = Redis::getRedis();
        $list = $redis->keys($text_key.'*');
        return empty($list)?'':$redis->get($list[0]);
    }

    public static function get_translate_val_to($model_name,$id,$field,$value){
        $m_lang = Language::getTotal();
        if($m_lang == 'zh'){
            return $value;
        }else{
            $val =  self::get_translate_val($model_name,$id,$field,$m_lang);
            return (!$val)?$value:$val;
        }
    }
//翻译入口
    public static function translate($query, $from, $to = null)
    {
        if($to == null){
            $to = Language::getTotal();
        }

        if($from == $to)
            return $query;
        $translate_key = 'translate_hash';
        $res_key = "{$query}_{$to}";
        $redis = Redis::getRedis();
        $res = $redis->hGet($translate_key,"{$query}_{$to}");
        if($res == FALSE) {
            $app_id = "20191118000358221";
            $sec_key = "7B2P6pWvA0BVW8UJSZW2";
            $args = array(
                'q' => $query,
                'appid' => $app_id,
                'salt' => rand(10000, 99999),
                'from' => $from,
                'to' => $to,
            );
            $args['sign'] = self::buildSign($query, $app_id, $args['salt'], $sec_key);
            $ret = self::call("http://api.fanyi.baidu.com/api/trans/vip/translate", $args);
            $ret = json_decode($ret, true);
            if(isset($ret['trans_result'][0]['dst'])){
                $res = $ret['trans_result'][0]['dst'];
                $redis->hSet($translate_key,$res_key,$res);
            }

        }
        return $res;
    }

//加密
    public static function buildSign($query, $appID, $salt, $secKey)
    {/*{{{*/
        $str = $appID . $query . $salt . $secKey;
        $ret = md5($str);
        return $ret;
    }/*}}}*/

//发起网络请求
    public static function call($url, $args=null, $method="post", $testflag = 0, $timeout = 10, $headers=array())
    {/*{{{*/
        $ret = false;
        $i = 0;
        while($ret === false)
        {
            if($i > 1)
                break;
            if($i > 0)
            {
                sleep(1);
            }
            $ret = self::callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }/*}}}*/

    public static function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = 10, $headers=array())
    {/*{{{*/
        $ch = curl_init();
        if($method == "post")
        {
            $data = self::convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        else
        {
            $data = convert($args);
            if($data)
            {
                if(stripos($url, "?") > 0)
                {
                    $url .= "&$data";
                }
                else
                {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(!empty($headers))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if($withCookie)
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }/*}}}*/

    public static function convert(&$args)
    {/*{{{*/
        $data = '';
        if (is_array($args))
        {
            foreach ($args as $key=>$val)
            {
                if (is_array($val))
                {
                    foreach ($val as $k=>$v)
                    {
                        $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                    }
                }
                else
                {
                    $data .="$key=".rawurlencode($val)."&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }/*}}}*/
}